package com.baizhi.service.impl;

import com.baizhi.dto.UserDTO;
import com.baizhi.entity.VoucherOrder;
import com.baizhi.mapper.VoucherOrderMapper;
import com.baizhi.service.IVoucherOrderService;
import com.baizhi.utils.IdGenerator;
import com.baizhi.utils.UserHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gange
 * @since 2023-04-06
 */
@Service
public class VoucherOrderServiceImpl extends ServiceImpl<VoucherOrderMapper, VoucherOrder> implements IVoucherOrderService {
    @Autowired
    private IdGenerator idGenerator;
    @Override
    public VoucherOrder selectOneByUserIdAndVoucherId(Long userId, Long voucherId) {
        return  query().eq("user_id",userId).eq("voucher_id",voucherId).one();
    }
    @Override
    @Transactional
    public  Long createOrder(Long voucherId){
        VoucherOrder voucherOrder = new VoucherOrder();
        Long aLong = idGenerator.generateId();
        voucherOrder.setId(aLong);
        voucherOrder.setCreateTime(LocalDateTime.now());
        voucherOrder.setStatus(1);//刚下单
        UserDTO user = UserHolder.getUser();
        System.out.println("user = " + user);
        voucherOrder.setUserId(UserHolder.getUser().getId());
        voucherOrder.setVoucherId(voucherId);
        save(voucherOrder);
        return aLong;
    }
}
